﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GUI
{
    public delegate void buttonRoomRefreshClickDelegate(Rooms room);
    public delegate void buttonRoomSendClickDelegate(Rooms room, string msg);
    public delegate void buttonTimeSendClickDelegate(DateTime date, DateTime time);
    public delegate void RefreshRoomDelegate(int roomNumber, string temperature);
    public delegate void RefreshTimeDelegate(
        string hour, string minute, string second, string year, string month, string date);

    public partial class FormMain : Form
    {
        public event buttonRoomRefreshClickDelegate buttonRoomRefreshClick;
        public event buttonRoomSendClickDelegate buttonRoomSendClick;
        public event buttonTimeSendClickDelegate buttonTimeSendClick;

        public FormMain()
        {
            InitializeComponent();
            timer1.Start();
        }

        private void buttonRoomRefresh_Click(object sender, EventArgs e)
        {
            if (sender.Equals(buttonRoom0Refresh))
            {
                if (buttonRoomRefreshClick != null) 
                    buttonRoomRefreshClick(Rooms.Room0);
            }

            else if (sender.Equals(buttonRoom1Refresh))
            {
                if (buttonRoomRefreshClick != null) 
                    buttonRoomRefreshClick(Rooms.Room1);
            }

            else if (sender.Equals(buttonRoom2Refresh))
            {
                if (buttonRoomRefreshClick != null) 
                    buttonRoomRefreshClick(Rooms.Room2);
            }

            else if (sender.Equals(refreshToolStripMenuItem))
            {
                if (buttonRoomRefreshClick != null)
                    buttonRoomRefreshClick(Rooms.All);
            }
        }

        private void buttonRoomSend_Click(object sender, EventArgs e)
        {
            if (sender.Equals(buttonRoom0Send))
            {
                if (buttonRoomSendClick != null) 
                    buttonRoomSendClick(Rooms.Room0, textBoxRoom0DesiredTemp.Text);
            }

            else if (sender.Equals(buttonRoom1Send))
            {
                if (buttonRoomSendClick != null) 
                    buttonRoomSendClick(Rooms.Room1, textBoxRoom1DesiredTemp.Text);
            }

            else if (sender.Equals(buttonRoom2Send))
            {
                if (buttonRoomSendClick != null)
                    buttonRoomSendClick(Rooms.Room2, textBoxRoom2DesiredTemp.Text);
            }
        }

        internal void RefreshRoomTemperature(int roomNumber, string temperature)
        {
            switch (roomNumber)
            {
                case 0: labelRoom0CurrentTemp.Text = temperature; break;
                case 1: labelRoom1CurrentTemp.Text = temperature; break;
                case 2: labelRoom2CurrentTemp.Text = temperature; break;
            }
        }

        internal void RefreshDesiredTemperature(int roomNumber, string temperature)
        {
            switch (roomNumber)
            {
                case 0: textBoxRoom0DesiredTemp.Text = temperature; break;
                case 1: textBoxRoom1DesiredTemp.Text = temperature; break;
                case 2: textBoxRoom2DesiredTemp.Text = temperature; break;
            }
        }

        internal void RefreshTime(string hour, string minute, string second, string year, string month, string date)
        {
            DateTime dt = DateTime.ParseExact(year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second, "y-M-d H:m:s", CultureInfo.InvariantCulture);
            datePicker.Value = dt;
            
            //DateTime time = DateTime.ParseExact(hour + ":" + minute + ":" + second, "H:m:s",
            //                            CultureInfo.InvariantCulture);
            timePicker.Value = datePicker.Value;
        }

        private void buttonSendTime_Click(object sender, EventArgs e)
        {
            if (buttonTimeSendClick != null) 
                buttonTimeSendClick(datePicker.Value, timePicker.Value);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timePicker.Value = timePicker.Value.AddSeconds(1);
            datePicker.Value = datePicker.Value.AddSeconds(1);
            labelTime.Text = timePicker.Value.ToLongTimeString() + " " + datePicker.Value.ToShortDateString();

        }
    }
}
